﻿Public Class table : Inherits element

    Public CellPadding As Integer = 0
    Public CellSpacing As Integer = 0

    Public columns As New List(Of column)

    Public Overrides Sub ResizeWidth(ByVal w As Integer)
        Dim wc = Style.width
        If Not wc.Contains("%") Then
            For Each col In columns
                Dim c = col
                col.width = (From tr In Contents From td As td In tr.Contents Where td.Column Is c Select td.MaxWidth).Max + CellPadding * 2
            Next
            Dim tw = (From c In columns Select c.width).Sum + CellSpacing * (columns.Count + 1)
            If tw <= w Then Exit Sub
        End If
        For Each col In (From c In columns Where c.style.fixedwidth)
            col.width = Val(col.style.width)
        Next
        Dim totalrelative = (From c In columns Where c.style.relativewidth Select Val(c.style.width)).Sum
        Dim totalfixed = (From c In columns Where c.style.fixedwidth Select Val(c.style.width)).Sum
        Dim emptycol = (From c In columns Where c.style.width = "").Count
        For Each col In (From c In columns Where c.style.width = "")
            col.style.width = Int((100 - totalrelative) / emptycol) & "%"
        Next
        For Each col In (From c In columns Where c.style.relativewidth)
            col.width = Val(col.style.width) * ((w - (columns.Count + 1) * CellSpacing) - totalfixed) / 100
        Next
        Dim totalwidth = (From c In columns Select c.width).Sum + (columns.Count + 1) * CellSpacing
        If totalwidth <> w Then
            Dim f = (From c In columns Where c.style.relativewidth).FirstOrDefault
            If Not f Is Nothing Then f.width += w - totalwidth
        End If
        For Each tr In Contents
            tr.ResizeWidth(w)
        Next
    End Sub

    Public Overrides Sub Draw(ByVal g As System.Drawing.Graphics, ByVal P As System.Drawing.Point)
        Dim R = New Rectangle(P.X, P.Y, Width, Height)
        Dim bgcolor = finalbgcolor
        If bgcolor <> "" Then
            g.FillRectangle(New SolidBrush(ColorTranslator.FromHtml("#" & bgcolor)), R)
        End If
        g.DrawRectangle(Pens.Red, R)
        For Each Item In Contents
            P.Offset(0, CellSpacing)
            Item.Draw(g, P)
            P.Offset(0, Item.Height)
        Next
    End Sub

    Public Overrides Sub SetAttributes(ByVal a As System.Collections.Generic.Dictionary(Of String, String))
        MyBase.SetAttributes(a)
        For Each Item In a
            Select Case Item.Key
                Case "cellpadding" : CellPadding = Item.Value
                Case "cellspacing" : CellSpacing = Item.Value
            End Select
        Next
    End Sub

    Sub EnsureColumns(ByVal i As Integer)
        For x = columns.Count To i
            Dim c As New column
            c.table = Me
            columns.Add(c)
        Next
    End Sub

    Public Overrides ReadOnly Property Height As Integer
        Get
            Return Contents.Sum(Function(x) x.Height) + (Contents.Count + 1) * CellSpacing
        End Get
    End Property

    Public Overrides ReadOnly Property Width As Integer
        Get
            Return Contents.Max(Function(x) x.Width)
        End Get
    End Property

End Class